package com.shouer.doc.ui.userinfo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.shouer.doc.R;
import com.shouer.doc.common.util.AppUtil;
import com.shouer.doc.common.util.DateFormatUtil;
import com.shouer.doc.common.util.RelativeDateFormatUtil;
import com.shouer.doc.ui.userinfo.model.BillModel;
import com.shouer.doc.ui.userinfo.view.IUserInfoView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by YYY on 2016/12/18.
 */

public class BillAdapter extends RecyclerView.Adapter<BillViewHolder> implements View.OnClickListener{

    private Context context;
    private List<BillModel> billModels;
    private LayoutInflater inflater;
    private IUserInfoView userInfoView;

    public BillAdapter(Context context,IUserInfoView userInfoView) {
        this.context = context;
        this.userInfoView=userInfoView;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public BillViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.fragment_userinfo_bill_item_layout, null);
        return new BillViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(BillViewHolder holder, int position) {

        BillModel billModel = billModels.get(position);
        holder.billDescView.setText(billModel.getBillDesc());
        try {
            holder.billTimeView.setText(RelativeDateFormatUtil.format(DateFormatUtil.stringToDate(billModel.getBillTime().replace("T", "\b"), "yyyy-MM-dd\bHH:mm:ss")));
        } catch (Exception e) {
            Log.d(BillAdapter.class.getSimpleName(), e.toString());
            holder.billTimeView.setText("");
        }

        holder.billAmountView.setText(billModel.getBillAmount());

        String icoUriString = null;
        if (billModel.getBillType().startsWith("1")) {
            if (billModel.getBillType().equals("11")) {
                icoUriString = "res://" + AppUtil.getPackageName(context) + "/" + R.drawable.ico_billtype_tw;
            } else if(billModel.getBillType().equals("12")){
                icoUriString = "res://" + AppUtil.getPackageName(context) + "/" + R.drawable.ico_order_type_audio;
            }else if (billModel.getBillType().equals("13"))
            {
                icoUriString = "res://" + AppUtil.getPackageName(context) + "/" + R.drawable.ico_order_type_video;
            }else if (billModel.getBillType().equals("14"))
            {
                icoUriString = "res://" + AppUtil.getPackageName(context) + "/" + R.drawable.ico_order_type_yao;
            }else
            {
                icoUriString = "res://" + AppUtil.getPackageName(context) + "/" + R.drawable.ico_billtype_tw;
            }
        } else {
            icoUriString = "res://" + AppUtil.getPackageName(context) + "/" + R.drawable.ico_billtype_tx;
        }
        holder.billTypeView.setImageURI(icoUriString);

        if (position == 0) {
            holder.titleView.setVisibility(View.VISIBLE);
        } else {
            holder.titleView.setVisibility(View.GONE);
        }

        holder.moreBill.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        if (null == billModels) {
            return 0;
        } else {
            return billModels.size();
        }
    }

    public void refreshBillList(List<BillModel> billModels) {
        this.billModels = billModels;
        notifyDataSetChanged();
    }

    public void attachToBillList(List<BillModel> billModels1) {
        if (null != billModels1) {
            if (null == billModels) {
                billModels = new ArrayList<>(0);
            }

            billModels.addAll(billModels1);
            notifyDataSetChanged();
        }
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public void onClick(View view) {
        userInfoView.onMoreBillClick();
    }
}
